import { defineStore } from "pinia";
import { AuthState } from "@/stores/interface";
import { getAuthMenuListApi, getCollectMenuListApi } from "@/api/modules/login";
import { collectMenuApi,getAllMenuVisitNum } from "@/api/modules/menu";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList, getCollectMenuList } from "@/utils";
import { sublevelRouter } from "@/routers/modules/sublevelRouter";
import { useGlobalStore } from "@/stores/modules/global";
import { saveUserConfig } from "@/api/modules/menu";
export const useAuthStore = defineStore({
	id: "geeker-auth",
	state: () : AuthState => ({
		// 菜单权限列表
		authMenuList: [],
		// 已收藏菜单列表
		collectMenuList: [],
		// 最近访问菜单列表
		recentsMenuList: [],
		//可累计菜单列表
		addressableMenuList:[],
		// 当前页面的 router name，用来做按钮权限筛选
		routeName: ""
	}),
	getters: {
		// 菜单权限列表 ==> 这里的菜单没有经过任何处理
		authMenuListGet: state => state.authMenuList,
		// 已收藏菜单列表
		collectMenuListGet: state => getCollectMenuList(state.collectMenuList),
		// 最近访问菜单列表recentsMenuListGet
		recentsMenuListGet: state => getCollectMenuList(state.recentsMenuList),
		//collectMenuItemGet: state => getCollectMenuList(state.recentsMenuList),
		// 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 is_hide == true
		showMenuListGet: state => getShowMenuList(state.authMenuList),
		// 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
		flatMenuListGet: state => getFlatMenuList(state.authMenuList),
		// 递归处理后的所有面包屑导航列表
		breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
	},
	actions: {
		// Get AuthMenuList
		async getAuthMenuList(id) {
			const { data } = await getAuthMenuListApi(id);
			//itemTwo
			sublevelRouter.map(sunlevelItem => {
				data.list.forEach((itemOne, indexOne) => {
					if (itemOne.children) {
						itemOne.children.forEach((itemTwo, indexTwo) => {
							if (itemTwo.children) {
								let tIdx = itemTwo.children.findIndex(itemThree => itemThree.path == sunlevelItem.path);
								if(tIdx != -1){
									if(itemTwo.children[tIdx].children){
										data.list[indexOne].children[indexTwo].children[tIdx].concat(sunlevelItem.children)
									} else {
										itemTwo.children[tIdx].children = sunlevelItem.children
									}
								}
								
							}
						})
					}
				})
			})
			data.list.map((itemOne, indexOne) => {
				if (itemOne.children) {
					itemOne.children.map((itemTwo, indexTwo) => {
						if (itemTwo.children) {
							itemTwo.children.map((itemThree, indexThree)=>{
								this.addressableMenuList.push({
									name:itemThree.name,
									path:itemThree.path,
									title:itemThree.meta.title,
									title_ru:itemThree.meta.title_ru,
								})
							})
						}
						
					})
				}
				
			})
			this.authMenuList = data.list;

		},
		// Get collectMenuList
		async getCollectMenuList() {
			const { data } = await getCollectMenuListApi();
			this.collectMenuList = data.fav_list;
			this.recentsMenuList = data.visited_list;
		},
		async getCollectMenuItem(id) {
			const { data } = await collectMenuApi({ id });
		},
		async getAllMenuVisitNum(path) {
			const { data } = await getAllMenuVisitNum({path});
		},
		// Set RouteName
		async setRouteName(name : string) {
			this.routeName = name;
		},
		//保存用户公共设置参数
		async saveUserValue() {
			let userSetMsg = {};
			const globalStore = useGlobalStore();
			userSetMsg = globalStore.$state;
			saveUserConfig({userSetMsg})
		},
		async saveUserPath(pathArr) {
			saveUserConfig(pathArr)
		},
		
	}
});